package com.qfedu.zeren;

import java.util.ArrayList;
import java.util.List;

// 数据清洗请求
class DataCleaningRequest {
    private String rawData;
    private List<String> stepsPerformed = new ArrayList<>();

    public DataCleaningRequest(String rawData) {
        this.rawData = rawData;
    }

    public String getRawData() {
        return rawData;
    }

    public void setRawData(String data) {
        this.rawData = data;
    }

    public List<String> getStepsPerformed() {
        return stepsPerformed;
    }

    public void addStep(String step) {
        stepsPerformed.add(step);
    }
}

// 数据清洗处理器
interface DataCleaner {
    void setNextCleaner(DataCleaner cleaner);

    void cleanData(DataCleaningRequest request);
}

// 抽象数据清洗器
abstract class AbstractDataCleaner implements DataCleaner {
    protected DataCleaner nextCleaner;

    @Override
    public void setNextCleaner(DataCleaner cleaner) {
        this.nextCleaner = cleaner;
    }

    @Override
    public void cleanData(DataCleaningRequest request) {
        // 每个处理器都执行自己的清洗逻辑
        processData(request);

        // 传递给下一个处理器,注意和第一个案例的写法进行比较
        // 第一个案例根据处理结果判断是否需要执行下一个处理器,而本案例只要下一个处理器不为空，则执行下一个处理器
        if (nextCleaner != null) {
            nextCleaner.cleanData(request);
        }
    }

    protected abstract void processData(DataCleaningRequest request);
}

// 具体清洗器 - 去除空格
class WhitespaceCleaner extends AbstractDataCleaner {
    @Override
    protected void processData(DataCleaningRequest request) {
        String data = request.getRawData();
        data = data.trim().replaceAll("\\s+", " ");
        request.setRawData(data);
        request.addStep("去除空格");
        System.out.println("空格清理完成: " + data);
    }
}

// 具体清洗器 - 特殊字符处理
class SpecialCharCleaner extends AbstractDataCleaner {
    @Override
    protected void processData(DataCleaningRequest request) {
        String data = request.getRawData();
        data = data.replaceAll("[^a-zA-Z0-9\\s]", "");
        request.setRawData(data);
        request.addStep("去除特殊字符");
        System.out.println("特殊字符清理完成: " + data);
    }
}

// 具体清洗器 - 大小写规范化
class CaseNormalizer extends AbstractDataCleaner {
    @Override
    protected void processData(DataCleaningRequest request) {
        String data = request.getRawData();
        data = data.toLowerCase();
        request.setRawData(data);
        request.addStep("大小写规范化");
        System.out.println("大小写规范化完成: " + data);
    }
}

// 具体清洗器 - 重复词去除
class DuplicateWordCleaner extends AbstractDataCleaner {
    @Override
    protected void processData(DataCleaningRequest request) {
        String data = request.getRawData();
        String[] words = data.split("\\s+");
        StringBuilder result = new StringBuilder();
        String lastWord = "";

        for (String word : words) {
            if (!word.equals(lastWord)) {
                result.append(word).append(" ");
                lastWord = word;
            }
        }

        request.setRawData(result.toString().trim());
        request.addStep("去除重复词");
        System.out.println("重复词清理完成: " + result.toString().trim());
    }
}

// 使用示例
public class DataCleaningDemo {
    public static void main(String[] args) {
        // 构建清洗链
        DataCleaner whitespaceCleaner = new WhitespaceCleaner();
        DataCleaner specialCharCleaner = new SpecialCharCleaner();
        DataCleaner caseNormalizer = new CaseNormalizer();
        DataCleaner duplicateCleaner = new DuplicateWordCleaner();

        whitespaceCleaner.setNextCleaner(specialCharCleaner);
        specialCharCleaner.setNextCleaner(caseNormalizer);
        caseNormalizer.setNextCleaner(duplicateCleaner);

        // 测试数据清洗
        String rawData = "  Hello   WORLD!!  Hello  TEST  123  ";
        DataCleaningRequest request = new DataCleaningRequest(rawData);

        System.out.println("原始数据: '" + rawData + "'");
        System.out.println("开始数据清洗...");

        whitespaceCleaner.cleanData(request);

        System.out.println("\n最终结果: '" + request.getRawData() + "'");
        System.out.println("执行步骤: " + request.getStepsPerformed());
    }
}